package com.easyexcel.demo.vo;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
import com.easyexcel.demo.common.SexEnum;
import com.easyexcel.demo.converter.EnumColumConvert;
import com.easyexcel.demo.converter.EnumFormat;
import com.easyexcel.demo.converter.LocalDateStringConverter;
import com.easyexcel.demo.converter.LocalDateTimeStringConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


/**
 * 表名：t_user
 *
 * @author zhaoxh
 * @since 2023-09-13 21:23:44
 */
@Data
@ApiModel(value = "返回值")
@ColumnWidth(20)
@ExcelIgnoreUnannotated
public class UserVO {

    /**
     *
     */
    @ApiModelProperty(value = "主键")
    private Long id;

    /**
     * 姓名
     */
    @ExcelProperty(value = "姓名", index = 0)
    @ApiModelProperty(value = "姓名")
    private String name;

    /**
     * 性别: 0未知 1男 2 女
     */
    @ExcelProperty(value = "性别", converter = EnumColumConvert.class, index = 1)
    @EnumFormat(value = SexEnum.class, columnCode = "code", columnName = "desc")
    @ApiModelProperty(value = "性别: 0未知 1男 2 女")
    private Integer sex;

    /**
     * 年龄
     */
    @ExcelProperty(value = "年龄", index = 2)
    @ApiModelProperty(value = "年龄")
    private Integer age;

    /**
     * 生日
     */
    @ApiModelProperty(value = "生日")
    @ExcelProperty(value = "生日", converter = LocalDateStringConverter.class, index = 3)
    private LocalDate birthday;

    /**
     * 薪水
     */
    @ApiModelProperty(value = "薪水")
    @ExcelProperty(value = "薪水", converter = BigDecimalStringConverter.class, index = 4)
    private BigDecimal salary;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @ExcelProperty(value = "创建时间", converter = LocalDateTimeStringConverter.class, index = 5)
    private LocalDateTime createTime;


    @ExcelIgnore
    private Map<String, DynamicField> dynamicFields = new TreeMap<>();

    // 动态字段结构
    @Data
    @AllArgsConstructor
    public static class DynamicField {
        /**
         * 老师
         */
        private int teacherIndex;
        private String teacherValue;
        /**
         * 课时
         */
        private int periodIndex;
        private String periodValue;
    }

    /**
     * 动态字段注册方法
     *
     * @param groupId 从 1 开始
     * @param teacher 老师名
     * @param period  课时
     */
    public void addDynamicField(int groupId, String teacher, String period) {
        validateGroupId(groupId);
        String key = "group_" + groupId;
        dynamicFields.put(key, new DynamicField(
                6 + 2 * (groupId - 1), // 动态计算索引
                teacher,
                7 + 2 * (groupId - 1), // 动态计算索引
                period
        ));
    }

    // 新增边界校验逻辑
    private void validateGroupId(int groupId) {
        if (groupId < 1) {
            throw new IllegalArgumentException("组别ID必须大于等于1");
        }
        // Excel最大列限制16384
        int maxCol = 6 + 2 * (groupId - 1);
        if (maxCol > 16383) {
            throw new IllegalArgumentException("超过Excel最大列限制");
        }
    }

}